<!DOCTYPE html>
<html >
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../plugins/layui/css/layui.css?v=${.Config.basic.version}" media="all">
    <link rel="stylesheet" href="../plugins/layui/css/style.css?v=${.Config.basic.version}" media="all">
    <link rel="stylesheet" href="../layuiadmin/style/admin.css?v=${.Config.basic.version}" media="all">
    <link rel="stylesheet" href="../plugins/zTree/css/metroStyle/metroStyle.css?v=${.Config.basic.version}">
    <link rel="stylesheet" href="../plugins/toastr/toastr.min.css?v=${.Config.basic.version}">
    <link rel="stylesheet" href="../plugins/font-awesome-4.7.0/css/font-awesome.min.css?v=${.Config.basic.version}" media="all">
    <link rel="stylesheet" href="//at.alicdn.com/t/font_2575369_9n08winu4t8.css?spm=a313x.7781069.1998910419.40&file=font_2575369_9n08winu4t8.css" media="all">
    <link rel="stylesheet" href="../css/ztree.css?v=${.Config.basic.version}" media="all">
    <style>
        .layui-fluid {
            padding: 15px;
        }
        .contextmenu {
            border-bottom: 0;
            background: #4958b9;
            position: absolute;
            list-style: none;
            margin: 0;
            padding: 0;
            display: none;
            z-index: 99999;
        }

        .contextmenu li{
            width: 160px;
        }

        .contextmenu li a {
            display: block;
            padding: 5px 20px;
            border-bottom: 1px solid #aaaaaa21;
            cursor: pointer;
            color: #fff;
        }
        .contextmenu li a:hover {
            background: #0f1644;
            color: #fff;
        }
        .toolbar{
            /*text-align: right;*/
            background: #fff;
        }

        .layui-card-body {
            position: relative;
            /*padding: 0;*/
            line-height: 24px;
        }
        .left-content .bg{
            width: 100%;
            height: 100%;
            background: url("/img/base/bg.png") no-repeat;
            background-size:100% 100%;
        }

        .left-content .root_block{
            width: 100%;
            height: 100%;
            background-color: #ccc;
        }

        .left-content .net_group_block{
            width: 100%;
            height: 100%;
            /*background-color: #f2f2f2;*/
        }
        .left-content .net_node_block{
            width: 100%;
            height: 100%;
            /*background-color: #f2f2f2;*/
        }
        .left-content .net_rack_block{
            width: 100%;
            height: 100%;

        }

        .scroll{
            overflow: auto;
        }
        .layui-form-select dl {
            max-height: 150px;
        }
    </style>
</head>
<body>
<div class="">
    <div style="display: flex;">
        <div style="width: 380px; ">
            <div class="panel-header">
                <span class="panel-title layui-text-mute">
                    <button type="button" onclick="loadTree();" class="fr mr-sm layui-btn layui-btn-sm layui-btn-normal"><i class="layui-icon close-btn layui-icon-refresh layui-text-white mr-sm"></i> 刷新</button>
                    <button type="button" class="fr mr-sm layui-btn layui-btn-sm layui-btn-normal"><i class=" layui-icon close-btn layui-icon-add-1 layui-text-white mr-sm"></i> 增加集团</button>  
                </span>
            </div>
            <div class="layui-card tree-div">

                <div class="">
                    <div class=" " >
                        
                        <ul id="treeDemo" class="ztree"></ul>
                    </div>
                </div>
            </div>
        </div>
        <div style="width: calc(100% - 380px);float: right; " >
            <div class="layui-card">

                <div class="layui-card-body">
                    <div class="left-content " >
                        <div class="bg" id="chart"></div>
                        <div style="display: none;" class="root_block">
                        </div>
                        <div style="display: none; background-color: #d8332a" class="farm_block">

                        </div>

                        <div style="display: none; " class="house_block">
                            <div class="toolbar">
                                <div class="layui-btn-group" style="float: right">
                                    <button type="button"  onclick="on_load_house();" class="action-view layui-btn layui-btn-primary layui-btn-sm"><i class="fa fa-refresh layui-text-light"></i></button>
                                    <button type="button"  onclick="on_save_house();" class="action-edit layui-hide layui-btn layui-btn-primary layui-btn-sm"><i class="fa fa-save layui-text-light"></i></button>
                                    <button type="button"  onclick="onRemove();" class="action-edit layui-hide layui-btn layui-btn-primary layui-btn-sm"><i class="fa fa-trash layui-text-light"></i></button>
                                </div>
                            </div>
                            <blockquote class="layui-elem-quote" >栏舍</blockquote>
                            <form class="layui-form" lay-filter="form_house" id="form_house" action="" style="padding: 0px;">
                                <div class="layui-form-item layui-hide">
                                    <label class="layui-form-label"></label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="id" lay-verify="" placeholder="" autocomplete="" class="layui-input" style="width: 100%">
                                    </div>
                                </div>


                                <div class="layui-form-item">

                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-large"><span class="layui-text-red">*</span> 名称</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="housename" value="" lay-verify="required" lay-verType="tips" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-large"><span class="layui-text-red">*</span> 类型</label>
                                        <div class="layui-input-inline">
                                            <select name="type" lay-filter="type" lay-verify="required"  lay-verType="tips" lay-search="">
                                                <option value="1" >妊娠舍</option>
                                                <option value="2" >哺乳舍</option>
                                                <option value="3" >保育舍</option>
                                                <option value="4" >育肥舍</option>
                                                <option value="5" >公猪舍</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>



                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-large">创建人</label>
                                        <div class="layui-input-inline">
                                            <input type="text" readonly name="create_user_name" value="" lay-verify="" lay-verType="tips" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-large">创建时间</label>
                                        <div class="layui-input-inline">
                                            <input type="text" readonly name="create_date" value="" lay-verify="" lay-verType="tips" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>

                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-large">最后操作人</label>
                                        <div class="layui-input-inline">
                                            <input type="text" readonly name="update_user_name" value="" lay-verify="" lay-verType="tips" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-large">更新时间</label>
                                        <div class="layui-input-inline">
                                            <input type="text" readonly name="update_date" value="" lay-verify="" lay-verType="tips" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>

                                </div>


                                <div class="layui-form-item layui-hide">
                                    <div class="layui-inline">
                                        <label class="layui-form-label"></label>
                                        <div class="layui-input-inline">
                                            <button type="button" class="layui-btn layui-btn-normal action-edit layui-hide" id="btn_save_house" lay-submit="btn_save_house" lay-filter="btn_save_house" style="float: right;">保存</button>
                                        </div>
                                    </div>
                                </div>

                            </form>

                        </div>

                        <div style="display: none; " class="node_block">
                            <div class="toolbar">
                                <div class="layui-btn-group" style="float: right">
                                    <button type="button"  onclick="onLoad();" class="action-view layui-btn layui-btn-primary layui-btn-sm"><i class="fa fa-refresh layui-text-light"></i></button>
                                    <button type="button"  onclick="onSave();" class="action-edit layui-hide layui-btn layui-btn-primary layui-btn-sm"><i class="fa fa-save layui-text-light"></i></button>
                                    <button type="button"  onclick="onRemove();" class="action-edit layui-hide layui-btn layui-btn-primary layui-btn-sm"><i class="fa fa-trash layui-text-light"></i></button>
                                </div>
                            </div>
                            <blockquote class="layui-elem-quote" ></blockquote>
                            <form class="layui-form" lay-filter="form" id="form" action="" style="padding: 0px;">
                                <div class="layui-form-item layui-hide">
                                    <input type="text" name="id" lay-verify="" placeholder="" autocomplete="" class="layui-input" style="width: 100%">
                                    <input type="text" name="pid" lay-verify="" placeholder="" autocomplete="" class="layui-input" style="width: 100%">
                                    <input type="text" name="type" lay-verify="" placeholder="" autocomplete="" class="layui-input" style="width: 100%">
                                </div>


                                <div class="layui-form-item">

                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-large"><span class="layui-text-red">*</span> 名称</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="name" value="" lay-verify="required" lay-verType="tips" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>

                                    <div class="layui-inline house_only">
                                        <label class="layui-form-label layui-form-label-large"><span class="layui-text-red">*</span> 类型</label>
                                        <div class="layui-input-inline">
                                            <select name="item_type" lay-filter="item_type" lay-verify=""  lay-verType="tips" lay-search="">
                                                <option value="1" >妊娠舍</option>
                                                <option value="2" >哺乳舍</option>
                                                <option value="3" >保育舍</option>
                                                <option value="4" >育肥舍</option>
                                                <option value="5" >公猪舍</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>


                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-large"> 联系人</label>
                                        <div class="layui-input-inline">
                                            <input type="text"  name="person" value="" lay-verify="" lay-verType="tips" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-large"> 联系电话</label>
                                        <div class="layui-input-inline">
                                            <input type="text"  name="phone" value="" lay-verify="" lay-verType="tips" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>

                                <div class="layui-form-item farm_only">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-large"> 省份</label>
                                        <div class="layui-input-inline">
                                            <select name="province" lay-filter="province" lay-verify=""  lay-verType="tips" lay-search="">
                                                <option value="" ></option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="layui-inline farm_only">
                                        <label class="layui-form-label layui-form-label-large"> 城市</label>
                                        <div class="layui-input-inline">
                                            <select name="city" lay-filter="city" lay-verify=""  lay-verType="tips" lay-search="">
                                                <option value="" ></option>
                                            </select>
                                        </div>
                                    </div>
                                </div>

                                <div class="layui-form-item farm_only">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-large"> 经度</label>
                                        <div class="layui-input-inline">
                                            <input type="text"  name="x" value="" lay-verify="" lay-verType="tips" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline ">
                                        <label class="layui-form-label layui-form-label-large"> 纬度</label>
                                        <div class="layui-input-inline">
                                            <input type="text"  name="y" value="" lay-verify="" lay-verType="tips" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>

                                <div class="layui-form-item farm_only">

                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-large"> 地址</label>
                                        <div class="layui-input-block">
                                            <textarea style="width: 515px;height: 66px;" type="text" name="address" value="" lay-verify="" lay-verType="tips" placeholder="" autocomplete="off" class="layui-input"></textarea>
                                        </div>
                                    </div>
                                </div>

                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-large">创建人</label>
                                        <div class="layui-input-inline">
                                            <input type="text" readonly name="create_user" value="" lay-verify="" lay-verType="tips" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-large">创建时间</label>
                                        <div class="layui-input-inline">
                                            <input type="text" readonly name="create_date" value="" lay-verify="" lay-verType="tips" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>

                                </div>

                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-large">最后操作人</label>
                                        <div class="layui-input-inline">
                                            <input type="text" readonly name="update_user" value="" lay-verify="" lay-verType="tips" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-large">更新时间</label>
                                        <div class="layui-input-inline">
                                            <input type="text" readonly name="update_date" value="" lay-verify="" lay-verType="tips" placeholder="" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>

                                </div>


                                <div class="layui-form-item layui-hide">
                                    <div class="layui-inline">
                                        <label class="layui-form-label"></label>
                                        <div class="layui-input-inline">
                                            <button type="button" class="layui-btn layui-btn-normal action-edit layui-hide" id="btn_save" lay-submit="btn_save" lay-filter="btn_save" style="float: right;">保存</button>
                                        </div>
                                    </div>
                                </div>

                            </form>

                        </div>

                    </div>
                </div>

            </div>
        </div>
    </div>
</div>



<script src="../plugins/layui/layui.js"></script>
<script src="../plugins/jquery/jquery-3.6.0.js?v=${.Config.basic.version}"></script>
<script src="../plugins/zTree/js/jquery.ztree.core.min.js?v=${.Config.basic.version}"></script>
<script src="../plugins/zTree/js/jquery.ztree.excheck.js?v=${.Config.basic.version}"></script>
<script src="../plugins/zTree/js/jquery.ztree.exedit.js?v=${.Config.basic.version}"></script>
<script src="../plugins/jquery.slimscroll.min.js"></script>
<script src="../plugins/toastr/toastr.min.js"></script>
<script src="../plugins/moment.min.js"></script>
<script src="../js/utils.js"></script>
<script src="../js/verify.js"></script>
<script src="../js/func.js?v=${.Config.basic.version}"></script>
<script src="../js/province.js"></script>

<script>
    var lay_index=null;
    var zTree=null;
    var table=null;
    var laydate=null;
    var form=null;
    var element=null;
    //var transfer=null;
    var actions=null;
    var token = window.localStorage.getItem("token");
    var user_info=JSON.parse(window.localStorage.getItem("user_info"));
    var is_admin = user_info.is_admin;
    var func_url="${.func_url}";
    var actions=[];
    var selectedTreeNode=null;
    var menu=null;
    var all_meters=[];

    var lay_index=null;

    var last_node=null;

    var provinceArr=[];
    var cityArr=[];
    for(var i=0;i<provinceData.length;i++){
        provinceArr.push(provinceData[i].name);
        var cityData = provinceData[i].city;
        for(var j=0;j<cityData.length;j++){
            cityArr.push(cityData[j].name);
        }
    }


    token = 'Bearer ' + token;
    layui.config({
        base: '../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table','form','iconPicker'], function(){
        $ = layui.$;
        form = layui.form;
        table = layui.table;
        form.verify({
            verify_ip:verify_ip,
        });
        toastr.options = {
            "closeButton": false, //是否显示关闭按钮
            "debug": false, //是否使用debug模式
            "positionClass": "toast-top-center",//弹出窗的位置
            "showDuration": "300",//显示的动画时间
            "hideDuration": "1000",//消失的动画时间
            "timeOut": "2000", //展现时间
            "extendedTimeOut": "1000",//加长展示时间
            "showEasing": "swing",//显示时的动画缓冲方式
            "hideEasing": "linear",//消失时的动画缓冲方式
            "showMethod": "fadeIn",//显示时的动画方式
            "hideMethod": "fadeOut" //消失时的动画方式
        };
        $.ajax({
            type: 'GET',
            url: '../Api/User/Actions',
            data: {func_url:func_url},
            beforeSend: function(request) {
                request.setRequestHeader("Authorization", 'Bearer '+window.localStorage.getItem("token"));
            },
            dataType: "json",
            success: function (data) {
                console.info(data)
                actions=data.data;
                for(var i=0;i<actions.length;i++){
                    $('.action-'+actions[i].action_id).removeClass("layui-hide");
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.msg('加载操作信息失败');
            }
        });

        form.on('submit(btn_save)', function(data){
            console.info(data.field);
            save(data.field);
        });
        form.on('submit(btn_add)', function(data){
            console.info(data.field);
            //return;
            save(data.field);
        });

        //加载tree
        loadTree();
        renderSelect();

        resizeElement();
    });

    function renderSelect(){
        //栏舍下拉
        var html="<option value=''></option>";
        for(var i=0;i<provinceArr.length;i++){
            html+="<option value=\""+provinceArr[i]+"\">"+provinceArr[i]+"</option>";
        }
        $("select[name='province']").html(html);


        html="<option value=''></option>";
        for(var i=0;i<cityArr.length;i++){
            html+="<option value=\""+cityArr[i]+"\">"+cityArr[i]+"</option>";
        }
        $("select[name='city']").html(html);

        form.render('select');

        form.on('select(province)', function(data){
            console.info(data.value);
            var matchs=$.grep(provinceData,function(item){
                return item.name == data.value;
            });

            if(matchs.length>0){
                var arr = matchs[0].city;
                html="<option value=''></option>";
                for(var i=0;i<arr.length;i++){
                    html+="<option value=\""+arr[i].name+"\">"+arr[i].name+"</option>";
                }
                $("select[name='city']").html(html);

                form.render('select');
            }

        });
    }

    var tree_setting = {
        view: {
            dblClickExpand: false,
            fontCss: getFont,
            nameIsHTML: true,
        },
        callback: {
            onClick: treeOnClick,
            //onRightClick: OnRightClick,
            //拖拽时鼠标放开事件
            //onDrop: treeOnDrop,
            //beforeDrag: treeBeforeDrag,
            //beforeExpand: zTreeBeforeExpand
            onExpand:treeOnExpand,
            onCollapse:treeOnCollapse
        },
        view: {
            dblClickExpand: false,
            fontCss: getFont,
            nameIsHTML: true,
            //addHoverDom: addHoverDom,
            //removeHoverDom: removeHoverDom,
            addDiyDom:addDiyDom
        },
        edit: {
            enable: true,
            showRemoveBtn: false,
            showRenameBtn: false,
            drag:{
                autoExpandTrigger: false,
                isCopy: false,
                prev: false,
                next: false,
                inner: false
            }
        },
        data: {
            simpleData: {
                enable: true,
                idKey: 'id',
                pIdKey: 'pid',
                rootPId: null
            }
        }
    };
    function addDiyDom(treeId, treeNode){
        selectedTreeNode = treeNode;
        var sObj = $("#" + treeNode.tId + "_span");
        if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
        var addStr = "<span class='button add' nodeid='"+treeNode.tId+"' id='addBtn_" + treeNode.tId
            + "' title='新增'' onfocus='this.blur();'></span>";

        var removeStr = "<span class='button remove' nodeid='"+treeNode.tId+"' id='removeBtn_" + treeNode.tId
            + "' title='删除' onfocus='this.blur();'></span>";

        if(treeNode.type=="group"||treeNode.type=="area"||treeNode.type=="farm"||treeNode.type=="house"||treeNode.type=="unit")
        {
            sObj.after(removeStr);
        }

        var btn = $("#removeBtn_"+treeNode.tId);
        if (btn) btn.bind("click", function(event){
            var tid = $(this).attr("nodeid");
            var node = zTree.getNodeByTId(tid);
            onRemove(node);
        });
        if(treeNode.type=="group"||treeNode.type=="area"||treeNode.type=="farm"||treeNode.type=="house")
        {
            sObj.after(addStr);
        }

        btn = $("#addBtn_" + treeNode.tId);
        if (btn) btn.bind("click", function (event) {
            var tid = $(this).attr("nodeid");
            var node = zTree.getNodeByTId(tid);
            onAdd(node);
            return false;
        });
    }

    function getFont(treeId, node) {
        return node.font ? node.font : {};
    }

    function treeOnClick(event, treeId, treeNode) {
        selectedTreeNode=treeNode;
        resizeElement();
        console.info(selectedTreeNode);
        console.info($(".farm_only").children("input"));
        $(".left-content").children("div").hide();

        $(".farm_only").hide();
        $(".house_only").hide();
        if(selectedTreeNode.type=="farm"){
            $(".farm_only").show();
            
        }else if(selectedTreeNode.type=="house"){
            $(".house_only").show();
        }else{
            $(".farm_only").children("input").attr("lay-verify", "");
            $(".farm_only").children("input").attr("lay-verify", "");
            $(".farm_only").children("input").attr("lay-verify", "");

            $(".house_only").children("input").attr("lay-verify", "");
            $(".house_only").children("input").attr("lay-verify", "");
            $(".house_only").children("input").attr("lay-verify", "");
        }
        $(".node_block").show();
        onLoad();
    }

    function treeOnExpand(event, treeId, treeNode) {
    }

    function treeOnCollapse(event, treeId, treeNode) {
        console.info(treeNode);
    }
    function deepCopy(obj) {
        if (typeof obj != 'object') {
            return obj;
        }
        var newobj = {};
        for (var attr in obj) {
            newobj[attr] = deepCopy(obj[attr]);
        }
        return newobj;
    }

    function onAdd(node) {
        var html="";
        if(node.type=="group"){
            html+="<option value='area'>团组/区域</option>";
            html+="<option value='farm'>养殖场</option>";
            html+="<option value='house'>栏舍/栋舍</option>";
            html+="<option value='unit'>单元</option>";
        }else if(node.type=="area"){
            html+="<option value='farm'>养殖场</option>";
            html+="<option value='house'>栏舍/栋舍</option>";
            html+="<option value='unit'>单元</option>";
        }else if(node.type=="farm"){
            html+="<option value='house'>栏舍/栋舍</option>";
            html+="<option value='unit'>单元</option>";
        }else if(node.type=="house"){
            html+="<option value='unit'>单元</option>";
        }
        $("select[name='type']").html(html);
        form.render('select');

        console.info(node);
        var obj={
            id:'',
            pid:node.id,
            name:'',
        };
        console.info(obj)

        form.val("form_add",obj);
        layer.open({
            //area: 'auto',
            area:['400px','450px'],
            type: 1,
            shade: 0.3,
            offset: '20%',
            shadeClose:true,
            title: '添加拓扑节点', //不显示标题
            content: $('#form_add'), //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
            btn: ['确定', '取消']
            ,yes: function(index, layero){
                lay_index=index;
                var submit = layero.find("#btn_add");
                submit.click();
            }
        });
    }

    function onRemove(node) {
        if(!node){
            node = selectedTreeNode;
        }
        layer.confirm('确定删除'+node.name+'?', function(index){
            layer.close(index); //再执行关闭
            layer.load(1);
            $.ajax({
                type: 'DELETE',
                url: '../Api/Org',
                data: {id:node.id},
                beforeSend: function(request) {
                    request.setRequestHeader("Authorization", 'Bearer '+window.localStorage.getItem("token"));
                },
                dataType: "json",
                success: function (res) {
                    if(res.code>=0){
                        toastr.success("删除成功");
                        last_node=null;
                        loadTree();
                    }else{
                        toastr.warning(res.msg, "异常");
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg('加载信息失败');
                },
                complete: function(XMLHttpRequest, textStatus) {
                    layer.closeAll('loading');
                },

            });
        });
    }

    function onLoad() {
        layer.load(1);
        $.ajax({
            type: 'GET',
            url: '../Api/Org',
            data: {id:selectedTreeNode.id},
            beforeSend: function(request) {
                request.setRequestHeader("Authorization", 'Bearer '+window.localStorage.getItem("token"));
            },
            dataType: "json",
            success: function (res) {
                if(res.code>=0){
                    if(res.count>0){
                        var obj=res.data[0];
                        console.info(obj);
                        form.val("form",obj);
                    }
                }else{
                    toastr.warning(res.msg, "异常");
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.msg('加载信息失败');
            },
            complete: function(XMLHttpRequest, textStatus) {
                layer.closeAll('loading');
            },

        });
    }

    function onSave() {
        console.info("on_save_farm")
        $("#btn_save").click();
    }
    
    function save(field) {
        var request = "";
        if(field.id.length>0){
            request = "PUT";
            delete field.create_user;
            delete field.create_date;
        }else{
            request = "POST";
            delete field.update_user;
            delete field.update_date;
        }
        layer.load(1);
        $.ajax({
            type: request,
            url: '../Api/Org',
            data: field,
            beforeSend: function(request) {
                request.setRequestHeader("Authorization", 'Bearer '+window.localStorage.getItem("token"));
            },
            dataType: "json",
            success: function (res) {
                if(res.code>=0){
                    toastr.success("保存成功");
                    if(request=="POST"){
                        layer.close(lay_index);
                    }

                    //更新last_node
                    last_node = {id:res.data};
                    console.info(last_node);
                    loadTree();
                }else{
                    toastr.warning(res.msg, "异常");
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.msg('加载信息失败');
            },
            complete: function(XMLHttpRequest, textStatus) {
                layer.closeAll('loading');
            },

        });
    }


    function save_house(field) {
        var request = "";
        if(field.id.length>0){
            request = "PUT";
            delete field.create_user;
            delete field.create_date;
        }else{
            request = "POST";
            delete field.update_user;
            delete field.update_date;
        }
        layer.load(1);
        $.ajax({
            type: request,
            url: '../Api/House',
            data: field,
            beforeSend: function(request) {
                request.setRequestHeader("Authorization", 'Bearer '+window.localStorage.getItem("token"));
            },
            dataType: "json",
            success: function (res) {
                if(res.code>=0){
                    toastr.success("保存成功");
                    if(request=="POST"){
                        layer.close(lay_index);
                    }

                    //更新last_node
                    last_node = {type:"house",id:res.data};
                    console.info(last_node);
                    loadTree();
                }else{
                    toastr.warning(res.msg, "异常");
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.msg('加载信息失败');
            },
            complete: function(XMLHttpRequest, textStatus) {
                layer.closeAll('loading');
            },

        });
    }

    function loadTree() {
        layer.load(1);
        $.ajax({
            type: 'GET',
            url: '../Api/Topo/GetTree',
            data: {},
            beforeSend: function(request) {
                request.setRequestHeader("Authorization", 'Bearer '+window.localStorage.getItem("token"));
            },
            dataType: "json",
            success: function (data) {
                //var data=data.data;
                var nodes=setDataIcon(data.data);
                console.info(nodes);
                zTree = $.fn.zTree.init($("#treeDemo"), tree_setting, nodes);
                if(is_admin){

                }else{
                    zTree.expandAll(true);
                }
                
                if(last_node){
                    //修改新增后，默认选中这个节点
                    var nodes = zTree.getNodesByParam("id", last_node.id, null);
                    if(nodes.length>0){
                        const node = nodes[0];
                        console.info(node);
                        zTree.selectNode(node);
                        zTree.setting.callback.onClick(null,null,node);//手动触发onClick事件
                    }
                }else{
                    var nodes = zTree.getNodesByParam("type", "root", null);
                    if(nodes.length>0){
                        const node = nodes[0];
                        console.info(node);
                        zTree.selectNode(node);
                        zTree.setting.callback.onClick(null,null,node);//手动触发onClick事件
                    }
                }

            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.msg('加载信息失败');
            },
            complete: function(XMLHttpRequest, textStatus) {
                layer.closeAll('loading');
            },
        });

    }


    //设置图标
    function setDataIcon(data){

        for(let i=0; i<data.length; i++){
            var node=data[i];

            node['font']={'color':'#333'};
            if(node.type=='group'){
                //node.name = "";
                node["icon"]="../plugins/zTree/css/metroStyle/img/net.png";
                //node.isParent=true;
                node.open = true;
            }
            else if(node.type=='area'){
                node["icon"]="../plugins/zTree/css/metroStyle/img/area.png";
                //node.isParent=true;
                node.open = true;
            }
            else if(node.type=='farm'){
                node["icon"]="../plugins/zTree/css/metroStyle/img/location.png";
                //node.isParent=true;
                node.open = false;
            }else if(node.type=='house'){
                node["icon"]="../plugins/zTree/css/metroStyle/img/house.png";
                //node.isParent=false;
            }else if(node.type=='unit'){
                node["icon"]="../plugins/zTree/css/metroStyle/img/unit.png";
                //node.isParent=false;
            }
            if(node['children']){
                var data1=node.children;
                setDataIcon(data1);
            }
        }
        return data;
    }
    $(window).resize(function () {
        //console.info('resize')
        resizeElement();
    });
    function resizeElement() {
        //$('.tree-div').height($(window).height()-60);
        $(".tree-div").slimScroll({
            height: $(window).height()-50,
            size: '4px', //组件宽度
            color: '#eee', //滚动条颜色
            position: 'right', //组件位置：left/right
            distance: '0px', //组件与侧边之间的距离
            start: 'top', //默认滚动位置：top/bottom
            opacity: .4, //滚动条透明度
            alwaysVisible: true, //是否 始终显示组件
            disableFadeOut: false, //是否 鼠标经过可滚动区域时显示组件，离开时隐藏组件
            railVisible: true, //是否 显示轨道
            railColor: '#333', //轨道颜色
            railOpacity: .2, //轨道透明度
            railDraggable: true, //是否 滚动条可拖动
            railClass: 'slimScrollRail', //轨道div类名
            barClass: 'slimScrollBar', //滚动条div类名
            wrapperClass: 'slimScrollDiv', //外包div类名
            allowPageScroll: true, //是否 使用滚轮到达顶端/底端时，滚动窗口
            wheelStep: 10, //滚轮滚动量
            touchScrollStep: 200, //滚动量当用户使用手势
            borderRadius: '7px', //滚动条圆角
            railBorderRadius: '7px' //轨道圆角
        });
    }

</script>

<script src="../js/check_screen.js?v=${.Config.basic.version}"></script>

</body>

<form class="layui-form" lay-filter="form_add" id="form_add" style="padding: 20px 30px 0 0;display:none;">
    <div class="layui-form-item layui-hide">
        <input type="text" name="id" lay-verify="" placeholder="" autocomplete="off" class="layui-input" style="width: 100%">
        <input type="text" name="pid" lay-verify="" placeholder="" autocomplete="off" class="layui-input" style="width: 100%">
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="layui-text-red">*</span> 名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" lay-verType="tips" placeholder="" autocomplete="off" class="layui-input" style="width: 100%">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="layui-text-red">*</span> 类型</label>
        <div class="layui-input-block">
            <select  name="type" lay-filter="type" lay-verify="required" lay-search=""  >
            </select>
        </div>
    </div>

    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="btn_add" id="btn_add" value="确认">
    </div>
</form>

<form class="layui-form" lay-filter="form_farm_add" id="form_farm_add" style="padding: 20px 30px 0 0;display:none;">
    <div class="layui-form-item layui-hide">
        <label class="layui-form-label"></label>
        <div class="layui-input-block">
            <input type="text" name="id" lay-verify="" placeholder="" autocomplete="off" class="layui-input" style="width: 100%">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="layui-text-red">*</span> 名称</label>
        <div class="layui-input-block">
            <input type="text" name="farmname" lay-verify="required" lay-verType="tips" placeholder="" autocomplete="off" class="layui-input" style="width: 100%">
        </div>
    </div>


    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="btn_add_farm" id="btn_add_farm" value="确认">
    </div>
</form>

<form class="layui-form" lay-filter="form_house_add" id="form_house_add" style="padding: 20px 30px 0 0;display:none;">
    <div class="layui-form-item layui-hide">
        <label class="layui-form-label"></label>
        <div class="layui-input-block">
            <input type="text" name="id" lay-verify="" placeholder="" autocomplete="off" class="layui-input" style="width: 100%">
        </div>
    </div>

    <div class="layui-form-item layui-hide">
        <label class="layui-form-label"></label>
        <div class="layui-input-block">
            <input type="text" name="farmid" lay-verify="required" placeholder="" autocomplete="off" class="layui-input" style="width: 100%">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="layui-text-red">*</span> 名称</label>
        <div class="layui-input-block">
            <input type="text" name="housename" lay-verify="required" lay-verType="tips" placeholder="" autocomplete="off" class="layui-input" style="width: 100%">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"><span class="layui-text-red">*</span> 类型</label>
        <div class="layui-input-block">
            <select name="type" lay-filter="type" lay-verify="required"  lay-verType="tips" lay-search="">
                <option value="1" >妊娠舍</option>
                <option value="2" >哺乳舍</option>
                <option value="3" >保育舍</option>
                <option value="4" >育肥舍</option>
                <option value="5" >公猪舍</option>
            </select>
        </div>
    </div>


    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="btn_add_house" id="btn_add_house" value="确认">
    </div>
</form>


</html>